#!/usr/bin/perl -w

use strict;
use warnings;

## initialize all sub components
require "sb.pl";
require "postaggerPipe.pl";
#require "npPipe.pl";

sub closeChunker
{
	postPipeClose();
	#npPipeClose();
}


## input:  a string to be chunked; can be multiple sentences
## output: a list of chunked sentences, one per entry
sub chunkText
{
	my $text = shift;
	return undef if(!$text);
	
	## split text into sentences, one per line
	my @sentences = sbProcessText($text) or return undef;
	my @returned;
	my $prob = 0; ## problem flag
	my ($tagged, $chunked);
	
	foreach my $s (@sentences)
	{
		## I've had a lot of problems with "garbage chemical sentences" as I like
		## to call them.  Essentailly, they are huge chemical formulae and
		## descriptions that don't yield me any useful info anyways.  Thus,
		## I've decided to exclude all sentences that are so big that they 
		## almost certainly couldn't be real sentences.  I'm almost certainly 
		## being generous with my length here, as the run-of-the-mill sentence
		## usually won't break 200 characters, although some sentences do run
		## into the 4k range
		if(length($s) > 6144) ## 6144 = 1024*6
		{
			print "!!!  Skipped sentence due to length:\n$s\n\n";
			next;
		} 
	
		## $s has no wrapping whitespace so we must add a newline to the
		## end so that the postagger knows where to stop
		postWrite($s . "\n");
		if(!postCanRead(120)) {$prob = 1; exit;} ## utterly ample time!
		if(!($tagged = postRead(120))) 
			{$prob = 1; exit;}  ## I don't even want to deal with misalignment issues if this doesn't read correctly anymore		 	
		#while(postCanRead(0.2))
		#{
		#	print "WHAT THE FUCK?!? (Excess output from posT\n";
		#	$tagged .= "\n".postRead(0.2);
		#	print "new tagged: \"$tagged\"\n";
		#}
	
		## note that the tagger ends its lines with \n so we don't have to
		#npWrite($tagged);
		#if(!npCanRead(10)) {$prob = 2; next;}
		#if(!($chunked = npRead(10))) {$prob = 2; next;} ## same as tagged
		$chunked = $tagged;
		
		#while(npCanRead(0.2))
		#{
		#	print "WHAT THE FUCK?!? (Excess output from NP\n";
		#	$chunked .= "\n".npRead(0.2);
		#	print "new tagged: \"$chunked\"\n";
		#}
		
		## chunking succeeded! add it to the output 
		push(@returned, $chunked);
	} 
	continue
	{
		if($prob)
		{
			print "\nWARNING: There was a problem TAGING sentence \"$s\"\n" if ($prob == 1);
			print "\nWARNING: There was a problem CHUNKING sentence \"$s\"\n" if($prob==2);
			
			## some debug info for our viewing pleasure
			print "   tagged: \"" . ($tagged ? $tagged : "UNDEF") . "\"\n";
			print "   chunked: \"" . ($chunked ? $chunked : "UNDEF") . "\"\n";
		
			## reset prob
			$prob = 0;
		}
	}
	
	return @returned;
}

sub phraseChunkedText
{
	
}

sub chunkerTest
{
	## taken from patent 6,500,032
	my $teststr = "Generally, a female metal terminal includes a tubular proximal end portion, and a pair of resilient contact piece portions which are separated from each other by slits extending longitudinally from a distal end of the terminal to the proximal end portion, and are supported by the proximal end portion in a cantilever manner. This female metal terminal is decreasing in diameter progressively from the proximal end portion toward the distal end, and a constricted portion, provided at the distal end portion, serves as a contact portion. When a bar-like male terminal is inserted into the female metal terminal through the distal end thereof, the male terminal contacts the contact portion of the female metal terminal, thereby creating an electrical connection therebetween. ";
	print "Chunker Test:\n";
	print "input:\n===\n\"$teststr\"\n===\n";
	my @ret = chunkText($teststr);
	my $output = join("\n\n", @ret);
	print "output:\n===\n\"$output\"\n===\n";
	print "End Test\n";
}

## tests just for fun
#print "### sbTest ###\n";
#sbTest();
#print "\n\n### postTest ###\n";
#postTest();
#print "\n\n### npTest ###\n";
#npTest();
#print "\n\n### chunkerTest ###\n";
#chunkerTest();
#closeChunker();
#print "\n### TESTS FINISHED ###\n";

1;

